#!/bin/bash
#Author: weike
#MyQQID: 2362317758
export TOPDIR=$PWD

export MODPKG_ROOT=${TOPDIR}/pkg
export MODSRC_ROOT=${TOPDIR}/src

export MODNAME=rc.local

export MODPKG_NAME=${MODNAME}.tar.gz
export MODSRC_NAME=${MODNAME}

export MODPKG_PATH=${MODPKG_ROOT}/${MODPKG_NAME}
export MODSRC_PATH=${MODSRC_ROOT}/${MODSRC_NAME}
export MODOUT_PATH=$TOPDIR/output

export SRV_CFGLIST_NAME=srv_conf.lst
export SRV_CFGHEAD_NAME=srv_conf.h
export SRV_CFGROOT_PATH=${MODSRC_PATH}/config

export SRV_CFGLIST_PATH=${SRV_CFGROOT_PATH}/${SRV_CFGLIST_NAME}
export SRV_CFGHEAD_PATH=${MODSRC_PATH}/${SRV_CFGHEAD_NAME}

#-----------------------------------------------------------------------
perro()
{
    echo -e "\033[47;41mERROR: $*\033[0m"
}

pwarn()
{
    echo -e "\033[47;43mWARN: $*\033[0m"
}

pinfo()
{
    echo -e "\033[47;44mINFO: $*\033[0m"
}

pdone()
{
    echo -e "\033[47;42mINFO: $*\033[0m"
}

#-----------------------------------------------------------------------

append_head()
{
	echo -e "#ifndef _SRV_CONF_H"    >  ${SRV_CFGHEAD_PATH}
	echo -e "#define _SRV_CONF_H "   >> ${SRV_CFGHEAD_PATH}
	echo -e "#include <ual_types.h>"  >> ${SRV_CFGHEAD_PATH}
	echo -e "ual_srv_node ual_srv_node_list[] = {" >> ${SRV_CFGHEAD_PATH}

}

append_tail()
{
	echo  "};"     >> ${SRV_CFGHEAD_PATH}
	echo  "#endif" >> ${SRV_CFGHEAD_PATH}
}

append_node_srv()
{
	echo  "    {"                          >> ${SRV_CFGHEAD_PATH}
	echo  "        .type   = srv,"          >> ${SRV_CFGHEAD_PATH}
	echo  "        .name   = \"${1}\","     >> ${SRV_CFGHEAD_PATH}
	echo  "        .enable = \"${2}\","     >> ${SRV_CFGHEAD_PATH}
	echo  "        .path   = \"${3}\","     >> ${SRV_CFGHEAD_PATH}
	echo  "        .args   = \"${4}\","     >> ${SRV_CFGHEAD_PATH}
	echo  "    },"                         >> ${SRV_CFGHEAD_PATH}
}

set_ual_srv()
{
	tmpline=$1
	is_srv_type=$(echo $tmpline | grep -w "type=srv")
	if [ -n "${is_srv_type}" ]; then
		arryline=$(echo $tmpline | sed 's/,/ /g')
		for kvgrp in $arryline
		do
			key=${kvgrp%%=*}
			val=${kvgrp##*=}
			if [ -n "${key}" ] && [ -n "${val}" ]; then
				if [ "xname" = "x${key}" ]; then
					export srv_name=${val}
				elif [ "xenable" = "x${key}" ]; then
					export srv_enable=${val}
				elif [ "xpath" = "x${key}" ]; then
					export srv_path=${val}
				elif [ "xargs" = "x${key}" ]; then
					export srv_args=${val}
				fi
			fi
		done
		if [ -n "${srv_name}" ] && [ -n "${srv_enable}" ] && [ -n "${srv_path}" ]; then
			append_node_srv  "${srv_name}" "${srv_enable}" "${srv_path}" "${srv_args}"
		fi
		unset srv_name
		unset srv_enable
		unset srv_path
		unset srv_args
	fi
}
parse_service_conf()
{
	while read line
	do
		procline=$(echo $line | sed 's/ //g')
		validline=$(echo $procline | grep -v "^#")
		validline=$(echo $validline | grep  "^type=")
		if [ -z "$validline" ]; then
			continue;
		fi
		set_ual_srv $procline
	done < ${SRV_CFGLIST_PATH}
}

if [ ! -e ${SRV_CFGLIST_PATH} ]; then
	perro "NO SRV_CFGLIST_PATH: ${SRV_CFGLIST_PATH}"
	exit 1
fi
pinfo "Start create: $SRV_CFGHEAD_PATH"
append_head
parse_service_conf
append_tail
pdone "Finish create: $SRV_CFGHEAD_PATH"


